﻿namespace PublicStaticClass
{
    using Microsoft.Office.Interop.Excel;
    using System;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class BuildNewExcel
    {
        [DllImport("user32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
        public static string outPutExcel(DataGridView datagrid, System.Data.DataTable dt, string saveAddress)
        {
            if (dt != null)
            {
                Exception exception;
                Microsoft.Office.Interop.Excel.Application application = new ApplicationClass();
                if (application == null)
                {
                    return "无法创建Excel对象，可能计算机未安装Excel";
                }
                Workbook workbook = application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                Worksheet after = null;
                for (int i = 0; i < 1; i++)
                {
                    if (after == null)
                    {
                        after = (Worksheet) workbook.Worksheets.Add(System.Type.Missing, System.Type.Missing, 1, System.Type.Missing);
                    }
                    else
                    {
                        after = (Worksheet) workbook.Worksheets.Add(System.Type.Missing, after, 1, System.Type.Missing);
                    }
                    Microsoft.Office.Interop.Excel.Range range = null;
                    if (i == 0)
                    {
                        long count = dt.Rows.Count;
                        long num3 = 0L;
                        float num4 = 0f;
                        after.Name = "明細資料一";
                        for (int j = 0; j < datagrid.Columns.Count; j++)
                        {
                            if (((datagrid.Columns[j].HeaderText != "编辑") && (datagrid.Columns[j].HeaderText != "删除")) && (datagrid.Columns[j].HeaderText != "选项"))
                            {
                                after.Cells[1, j + 1] = datagrid.Columns[j].HeaderText;
                                range = (Microsoft.Office.Interop.Excel.Range) after.Cells[1, j + 1];
                                range.Interior.ColorIndex = 15;
                                range.Font.Bold = true;
                                range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                                range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
                                range.ColumnWidth = 6.63;
                                range.EntireColumn.AutoFit();
                                range.EntireRow.AutoFit();
                            }
                        }
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            for (int m = 0; m < datagrid.Columns.Count; m++)
                            {
                                string dataPropertyName = datagrid.Columns[m].DataPropertyName;
                                if (dataPropertyName != "")
                                {
                                    string str2 = dt.Rows[k][dataPropertyName].ToString();
                                    after.Cells[k + 2, m + 1] = str2;
                                    range = (Microsoft.Office.Interop.Excel.Range) after.Cells[k + 2, m + 1];
                                    range.Font.Size = 9;
                                    range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
                                    range.EntireColumn.AutoFit();
                                }
                            }
                            num3 += 1L;
                            num4 = ((float) (100L * num3)) / ((float) count);
                        }
                        range.Borders[XlBordersIndex.xlInsideHorizontal].Weight = XlBorderWeight.xlThin;
                        if (dt.Columns.Count > 1)
                        {
                            range.Borders[XlBordersIndex.xlInsideVertical].Weight = XlBorderWeight.xlThin;
                        }
                    }
                }
                try
                {
                    string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                    if (!Directory.Exists(baseDirectory + "Excel"))
                    {
                        Directory.CreateDirectory(baseDirectory + "Excel");
                    }
                    workbook.SaveCopyAs(saveAddress);
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    return ("生成Excel附件過程中出現異常，詳細信息如：" + exception.ToString());
                }
                try
                {
                    if (application != null)
                    {
                        int num8;
                        GetWindowThreadProcessId(new IntPtr(application.Hwnd), out num8);
                        Process.GetProcessById(num8).Kill();
                    }
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    return exception.Message.ToString();
                }
            }
            return "";
        }
    }
}

